【農家は Replace() されました】迷路_03
概要
複数のドローンを使って、右手の法則と左手の法則を用いて迷路を探索するコード。
https://gyazo.com/6cbb78f519230f7e620ed50a7921bfe4
ソースコード
code:rl_gold_harvest.python
clear()
size = get_world_size()
set_world_size(size)
directions = North, East, South, West #北, 東, 南, 西
right_hand_offsets = 1, 0, -1, -2 # 右, 前, 左, 後
left_hand_offsets = -1, 0, 1, 2 # 左, 前, 右, 後
def make_maze():
plant(Entities.Bush)
substance = size * 2**(num_unlocked(Unlocks.Mazes) - 1)
use_item(Items.Weird_Substance, substance)
def next_index(i, offsets):
for o in offsets:
ni = (i + o) % directions.len()
if can_move(directionsni):
return ni
def drone_run(index, offsets):
while True:
index = 0
while get_entity_type() != Entities.Treasure:
index = next_index(index, offsets)
move(directionsindex)
harvest()
make_maze()
def right_hand_drone_run():
right_index = 0
drone_run(right_index, right_hand_offsets)
def left_hand_drone_run():
left_index = 0
drone_run(left_index, left_hand_offsets)
for i in range(max_drones()):
move(North)
if i % (max_drones() // size) == 0:
for j in range(size // 4 + 1):
move(East)
if i % 2 == 0:
spawn_drone(right_hand_drone_run)
else:
spawn_drone(left_hand_drone_run)
make_maze()
while True:
right_hand_drone_run()
コードの解説
いわゆる、右手の法則と左手の法則を併用
メインルーチン
迷路の基準サイズをワールドサイズに設定
方向配列(北, 東, 南, 西)を定義
探査優先順位を定義
右手の法則:(右, 前, 左, 後)
右手の法則:(左, 前, 右, 後)
子機ドローンをいい感じにばら撒きながら生成
偶数番は右手法、奇数番は左手法のドローンを生成する。
複数ドローンが左右手法を併用して並列探索し、迷路を面でカバーする。
迷路を生成
無限ループ
親機のドローンも右手法で動かす
ドローンの挙動
無限ループ
宝箱を見つけるまでループ
進行方向の決定
移動
宝箱を収穫
迷路を生成
進行方向の決定
現在の向きを基準に、探査優先順位に次の方向を判定。
移動可能な方向を見つけた時点で、その方向インデックスを返す。
#農家は_Replace()_されました